<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
      <el-breadcrumb separator="">
        <el-breadcrumb-item
          ><div class="div1">仓库管理</div></el-breadcrumb-item
        >
        <el-breadcrumb-item :to="{ path: '/addGoodsWareHouse' }"
          ><div class="div2">商品仓库</div></el-breadcrumb-item
        >
      </el-breadcrumb>
    </div>
    <!-- 面包屑导航 -->
    <!-- 添加客户等级 -->
    <div class="content">
      <p class="header">添加仓库</p>
      <el-form :inline="true" :model="formInline1" class="demo-form-inline">
        <el-form-item label="商品仓库">
          <el-input
            maxlength="32"
            v-model="formInline1.name"
            placeholder="请输入仓库名称"
          ></el-input>
        </el-form-item>

        <!-- <el-form-item label="仓库管理员">
          <el-input
            v-model="formInline.keeper"
            placeholder="请输入仓库名称"
          ></el-input>
        </el-form-item> -->

        <el-form-item>
          <el-button type="primary" @click="listAxios(1)">搜索</el-button>

          <el-button
            type="primary"
            @click="
              dialoVisible = true;
              formInline = {};
            "
            v-if="parArray.indexOf('wareAdd') != -1"
            >添加仓库</el-button
          >
          <!-- onSubmit -->
        </el-form-item>
      </el-form>
    </div>
    <!-- 添加客户等级 -->
    <!-- 管理客户等级 -->
    <div class="manage">
      <p class="header">管理分类</p>
      <div class="table">
        <el-table :data="tableData.list">
          <el-table-column
            label="序号"
            type="index"
            width="50px;"
          ></el-table-column>
          <el-table-column scope="scope" label="仓库名称" width="200">
            <template slot-scope="scope">
              <el-input
                maxlength="32"
                v-if="scope.row.edit"
                v-model="scope.row.name"
              ></el-input>
              <span v-else>{{ scope.row.name }}</span>
            </template>
          </el-table-column>

          <el-table-column scope="scope" label="破损仓库名称" width="200">
            <template slot-scope="scope">
              <el-input
                maxlength="52"
                v-if="scope.row.edit"
                v-model="scope.row.damagedName"
              ></el-input>
              <span v-else>{{ scope.row.damagedName }}</span>
            </template>
          </el-table-column>

          <el-table-column scope="scope" label="仓库管理员" width="200">
            <template slot-scope="scope">
              <el-input
                maxlength="52"
                v-if="scope.row.edit"
                v-model="scope.row.keeper"
              ></el-input>
              <span v-else>{{ scope.row.keeper }}</span>
            </template>
          </el-table-column>

          <el-table-column
            label="更新时间"
            prop="createdTime"
          ></el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div class="optain">
                <p
                  @click="tolocation(scope.row)"
                  v-if="parArray.indexOf('locationCode') != -1"
                >
                  <span>库位</span>
                </p>

                <p
                  @click="scope.row.edit = !scope.row.edit"
                  v-if="!scope.row.edit && parArray.indexOf('warebc') != -1"
                >
                  <span>编辑</span>
                </p>

                <p
                  @click="fixHouser(scope.row)"
                  v-if="scope.row.edit && parArray.indexOf('warebc') != -1"
                >
                  <span>保存</span>
                </p>

                <p
                  @click="
                    (scope.row.edit = !scope.row.edit),
                      (scope.row.name = scope.row.name1),
                      (scope.row.keeper = scope.row.keeper1),
                      (scope.row.damagedName = scope.row.damagedName1)
                  "
                  v-if="scope.row.edit && parArray.indexOf('warebc') != -1"
                >
                  <span>取消</span>
                </p>

                <!-- <el-tooltip
                  class="item"
                  effect="dark"
                  content="向下移动"
                  placement="top-start"
                >
                  <p v-if="parArray.indexOf('wareBottom') != -1">
                    <span>下移</span>
                  </p>
                </el-tooltip> -->
                <!-- <el-tooltip
                  class="item"
                  effect="dark"
                  content="向上移动"
                  placement="top-start"
                >
                  <p v-if="parArray.indexOf('wareTop') != -1">
                    <span>上移</span>
                  </p>
                </el-tooltip> -->
                <p
                  @click="delect(scope.row, scope.$index)"
                  v-if="parArray.indexOf('wareDelect') != -1"
                >
                  <span>删除</span>
                </p>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <p style="height: 30px"></p>
      </div>
    </div>
    <!-- 管理客户等级 -->
    <!-- 弹框  -->
    <el-dialog
      :visible.sync="dialoVisible"
      :append-to-body="true"
      title="新增仓库"
    >
      <div class="form">
        <el-form label-position="right" label-width="80px" :model="formInline">
          <el-form-item label="仓库名称">
            <el-input v-model="formInline.name" maxlength="30"></el-input>
          </el-form-item>
          <el-form-item label="破损仓库">
            <el-input
              v-model="formInline.damagedName"
              maxlength="30"
            ></el-input>
          </el-form-item>
          <el-form-item label="管理员">
            <el-input v-model="formInline.keeper" maxlength="30"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="dialoVisible = false"
            >取 消</el-button
          >
          <el-button size="small" type="primary" @click="onSubmit()"
            >确认</el-button
          >
        </div>
      </div>
    </el-dialog>
    <!-- 弹框  -->
  </div>
</template>

<script>
export default {
  data () {
    return {
      formInline: {
        name: "",
        keeper: "",
        damagedName: "",
      },
      formInline1: {
        name: "",
      },
      tableData: [],
      limits: "",
      parArray: [],
      dialoVisible: false,
    };
  },
  created () {
    this.listAxios("1");
    // 权限设定判断
    this.limits = JSON.parse(sessionStorage.getItem("limit"));
    const params = this.limits.buttonTreeDto.filter(
      (item) => item.menuCode === "WareHouseCode"
    );
    params[0].buttonList.forEach((item) => {
      this.parArray.push(item.code);
    });
    // 权限设定判断
  },
  activated () {
    // this.listAxios("1");
  },
  methods: {
    // 添加仓库
    onSubmit () {
      if (this.formInline.name == "") {
        this.$message.warning("仓库名字不能为空");
      } else if (this.formInline.keeper == "") {
        this.$message.warning("仓库管理人不能为空");
      } else if (this.formInline.damagedName == "") {
        this.$message.warning("破损仓字段不能为空");
      } else {
        this.axios
          .post(
            "/order/comm/commWarehouse/add",
            {
              createdBy: sessionStorage.getItem("user_id"),
              name: this.formInline.name,
              keeper: this.formInline.keeper,
              sort: 0,
              damagedName: this.formInline.damagedName,
            },
            {
              header: {
                isloading: false,
              },
            }
          )
          .then((res) => {
            // console.log(res);
            if (res.code == 0) {
              this.$message.success("添加成功");
              this.listAxios("1");
              this.dialoVisible = false;
            } else {
              this.$message.error(res.message);
            }
          });
      }
    },
    // 库位跳转
    tolocation (row) {
      this.$router.push({
        name: "location",
        query: {
          content: JSON.stringify(row),
        },
      });
    },
    // 仓库列表
    listAxios (pageNo) {
      this.axios
        .post("/order/comm/commWarehouse/list", {
          name: this.formInline1.name,
          pageNo: pageNo,
          pageSize: 50,
        })
        .then((res) => {
          // console.log(res);
          if (res.code == 0) {
            for (let i in res.data.list) {
              res.data.list[i].edit = false;
              res.data.list[i].name1 = res.data.list[i].name;
              res.data.list[i].keeper1 = res.data.list[i].keeper;
              res.data.list[i].damagedName1 = res.data.list[i].damagedName;
            }
            this.tableData = res.data;
          } else {
            this.$message.error(res.message);
          }
        });
    },
    // 修改仓库名
    fixHouser (row) {
      if (!row.name || !row.keeper || !row.damagedName) {
        this.$message.warning("不能为空");
        return;
      }
      this.axios
        .post(
          "/order/comm/commWarehouse/update",
          {
            name: row.name,
            keeper: row.keeper,
            id: row.id,
            sort: 0,
            updatedBy: sessionStorage.getItem("user_id"),
            damagedName: row.damagedName,
          },
          {
            header: {
              isloading: false,
            },
          }
        )
        .then((res) => {
          // console.log(res);
          this.listAxios("1");
          if (res.code == 0) {
            this.$message.success("修改成功");
          } else {
            this.$message.error(res.message);
          }
        });
    },
    /***删除数据对接***/
    delect (row, index) {
      this.$confirm("确定要删除该仓库吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axios
            .get("/order/comm/commWarehouse/delete?id=" + row.id)
            .then((res) => {
              console.log(res);
              if (res.code == 0) {
                this.tableData.list.splice(index, 1);
                this.$message.success("删除成功");
                this.listAxios("1");
              } else {
                this.$message.error(res.message);
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    /***删除数据对接***/
  },
};
</script>

<style scoped lang="scss">
.div1 {
  margin-left: -16px;
  background: url(../../../assets/breadcrumb2.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #2d7edf;
}
.div2 {
  margin-left: -16px;
  background: url(../../../assets/breadcrumb1.png);
  width: 120px;
  height: 30px;
  text-align: center;
  line-height: 30px;
  color: #fff;
}
.content {
  padding: 10px;
  background: #fff;
  .el-button {
    padding: 6px 10px;
  }
}
.header {
  font-size: 16px;
  color: #2d7edf;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}
.demo-form-inline {
  margin-top: 10px;
}
/deep/.el-input__inner {
  height: 30px;
  line-height: 30px;
}
.manage {
  margin-top: 20px;
  padding: 10px;
  background: #fff;
}
/deep/.has-gutter .cell {
  text-align: center;
  color: #2d7ddf;
}
/deep/.cell {
  text-align: center;
}
/deep/.el-table__row .cell {
}
.optain {
  width: 100%;
  text-align: center;
}
.name {
  font-size: 16px;
  color: #2d7edf;
  margin: 10px 0;
}
.optain {
  p {
    display: inline-block;
    padding: 0 5px;
    img {
      width: 20px;
      height: 20px;
    }
  }
  p:hover {
    cursor: pointer;
  }
}
.dialog-footer {
  text-align: right;
}
</style>
